<?php
namespace Admin\Controller;
use Think\Controller;
class SystemController extends CommonController {
    //封装配置组方法
	protected function get_item($group = null){
        //根据传入的组名查询
	    if ($group !== false) {
	        $itme_arr = array();
	        $temp     = M('configinfo')->where(array('group' => $group))->order('id asc')->select();
	        foreach ($temp as $key => $v) {
	            $itme_arr[$v['value']] = $v['name'];
	        }
	    }
	    return $itme_arr;
	}
    //加载配置列表页
	public function index(){
		$vlist = M("configs")->order('id')->select();
        if (!$vlist) {
            $vlist = array();
        }
        //调用配置组
        $configgroup = $this->get_item('configgroup');
        $glist = array();
        //按配置组 遍历系统配置里面的配置
        foreach ($configgroup as $k => $v) {
            $glist[$k] = array();
            foreach ($vlist as $k2 => $v2) {
                if ($k == $v2['groupid']) {
                    $glist[$k][] = $v2;
                }
            }
        }
        $this->assign('vlist', $glist);
        $this->assign('configgroup', $configgroup);
        $this->display();

	}
    //执行修改
	public function site(){
		$data = I('config', array(), 'trim');
        $data['CFG_IMGTHUMB_SIZE'] = strtoupper($data['CFG_IMGTHUMB_SIZE']);
        if (empty($data['CFG_WEBNAME'])) {
            alertMes('网站名称不能为空','/admin/system/index');
        }
        if (empty($data['CFG_WEBTITLE'])) {
            alertMes('网站标题不能为空','/admin/system/index');
        }
        if (empty($data['CFG_IMGTHUMB_NUM'])) {
            alertMes('多图上传数量不能为空','/admin/system/index');
        }
        foreach ($data as $k => $v) {
             $ret = M('configs')->where(array('name' => $k))->save(array('value' => $v));
        }
        if ($ret !== false) {
            alertMes('系统配置成功','/admin/system/index');
        } else {
            alertMes('系统配置失败');
        }
	}
    //加载配置管理
    public function config(){
        $groupid = I('groupid', 0, 'intval'); //组别ID
        $where=array();
        if (!empty($groupid)) {
            $where['groupid'] = $groupid;
        }
        $mod=M("configs");
        //实例化分页类
        $page=new \Think\Page($mod->where($where)->Count(),10);
        //分页设置
        $page->setConfig('prev','上一页');
        $page->setConfig('next','下一页');
        $page->setConfig('first','首页');
        $page->setConfig('last','末页');
        $page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $vlist = $mod->where($where)->limit($page->firstRow,$page->listRows)->order('id DESC')->select();
        $this->assign('groupid', $groupid);
        $this->assign('page',$page->show());//分页显示
        $this->assign('configgroup', get_item('configgroup'));
        $this->assign('vlist', $vlist);
        $this->display();
    }
    //添加配置项
    public function add(){
        if (IS_POST) {
            $data            = I('post.');
            $data['groupid'] = I('groupid', 0, 'intval');
            $data['typeid']  = I('typeid', 0, 'intval');
            if (empty($data['name'])) {
                alertMes('配置项名称不能为空');
            }
            if (empty($data['title'])) {
                alertMes('配置项标题不能为空');
            }
            if (!preg_match('/^[a-zA-Z0-9_]+$/', $data['name'])) {
                alertMes('配置项名称只能由字母、数字和"_"组成');
            }
            //配置项名称转换为大写
            $data['name'] = strtoupper($data['name']);
            if (M('configs')->where(array('name' => $data['name']))->find()) {
               alertMes('配置项名称(标识)已经存在，请更换');
            }
            if (M('configs')->add($data)) {
                alertMes('添加成功','/admin/system/config');
            } else {
               alertMes('添加失败');
            }

            exit();
        }

        $this->assign('configgroup', get_item('configgroup'));
        $this->assign('configtype', get_item('configtype'));
        $this->display();
    }

    //配置管理修改
    public function edit(){
        $id = I('id', 0, 'intval');
        if (IS_POST) {
            $data            = I('post.');
            $id              = $data['id']              = I('id', 0, 'intval');
            $data['groupid'] = I('groupid', 0, 'intval');
            $data['typeid']  = I('typeid', 0, 'intval');
            if (empty($data['name'])) {
                alertMes('配置项名称不能为空');
            }
            if (empty($data['title'])) {
                alertMes('配置项标题不能为空');
            }
            if (!preg_match('/^[a-zA-Z0-9_]+$/', $data['name'])) {
                alertMes('配置项名称只能由字母、数字和"_"组成');
            }
            $data['name'] = strtoupper($data['name']);
            if (M('configs')->where(array('name' => $data['name'], 'id' => array('neq', $id)))->find()) {
                alertMes('配置项名称(标识)已经存在，请更换');
            }
            if (false !== M('configs')->save($data)) {
                alertMes('修改成功','/admin/system/config');
            } else {
                alertMes('修改失败');
            }
        }
        $vo          = M('configs')->find($id);
        //把预定义的字符转换为 HTML 实体
        $vo['value'] = htmlspecialchars($vo['value']);
        $this->assign('vo', $vo);
        $this->assign('configgroup', get_item('configgroup'));
        $this->assign('configtype', get_item('configtype'));
        $this->display();
    }

     //删除配置项
    public function del(){
        $id      = I('id', 0, 'intval');
        $groupid = I('groupid', 0, '');
        if (empty($id)) {
            alertMes('参数错误!');
        }
        //执行删除
        if (M('configs')->delete($id)) {
            alertMes('删除成功','/admin/system/config');
        } else {
            alertMes('删除失败');
        }
    }

}